package cn.cjy.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import bean.Flower;
import cn.cjy.model.Picture;
import service.FlowerService;
import util.UploadFileUtil;

/**
 * Servlet implementation class AdminAddPictureServlet
 */
@WebServlet("/AdminAddPictureServlet")
public class AdminAddPictureServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminAddPictureServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flower_id=request.getParameter("flower_id");
		String flower_name=request.getParameter("flower_name");
		String quantity=request.getParameter("quantity");
		String price=request.getParameter("price");
		String describes=request.getParameter("describes");
		String picture=request.getParameter("picture");
		Picture p=new Picture();
		f.setFlower_id(Integer.parseInt(flower_id));
		f.setFlower_name(flower_name);
		f.setQuantity(Integer.parseInt(quantity));
		f.setPrice(Double.parseDouble(price));
		f.setDescribes(describes);
		f.setPicture(picture);
		
		Part part=request.getPart("picture");
		System.out.println("part--file"+part);
		UploadFileUtil uploadFileUtil=new UploadFileUtil();
		uploadFileUtil.setPart(part);
		uploadFileUtil.setPath("D:/images");
		int savefile=uploadFileUtil.saveFile();
		if(savefile>=1) {
			f.setPicture(uploadFileUtil.getFilename());
		}
		
		
		FlowerService flowerService=new FlowerService();
		int n=flowerService.insertFlower(f);
		
		System.out.println("servlet里n="+n);
		if(n>=1) {
			request.setAttribute("infomation", "插入成功");
			request.setAttribute("url", "SelectAllFlowerServlet");
			request.setAttribute("second", 3);
			RequestDispatcher dispatcher=request.getRequestDispatcher("tip.jsp");
			dispatcher.forward(request, response);
		}else {
			request.setAttribute("infomation", "插入失败");
			request.setAttribute("url", "insertFlower.jsp");
			request.setAttribute("second", 3);
			RequestDispatcher dispatcher=request.getRequestDispatcher("tip.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
